package pers.ht.mbpo.service;

import net.sf.json.JSONArray;
import pers.ht.mbpo.bean.BusCar;
import pers.ht.mbpo.bean.BusCarReturn;
import pers.ht.mbpo.dao.BusCarDao;
import pers.ht.mbpo.util.ExcelUtil;
import pers.ht.mbpo.util.UploadUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/BusCServlet")
public class BusCServlet extends HttpServlet {
    private BusCarDao bDao = new BusCarDao();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String action = request.getParameter("action");
        if ("upload".equals(action)) {
            PrintWriter out = response.getWriter();

            out.println(UploadUtil.getJson());
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST, DELETE,PUT,GET,OPTIONS");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.addHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/xml");
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        String time = request.getParameter("time");
        String carid = request.getParameter("carid");
        String busLineName = request.getParameter("busLineName");
        String updown = request.getParameter("updown");
        List<BusCarReturn> carList = bDao.query(time, busLineName, updown,carid);
        String scoreJson = JSONArray.fromObject(carList).toString();
        out.print(scoreJson);
    }
}
